Grokking Microservices Design Patterns
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
The Course at a Glance
Strangler Fig Pattern
Introduction
The Problem: Legacy Systems
The Strangler Pattern: A Solution
The Architecture of the Strangler Pattern
Strangler Pattern: A Detailed Example
Key Insights and Implications
API Gateway Pattern
Introduction to the API Gateway Pattern
Advantages of API Gateway Pattern
API Gateway Pattern: An Example
Performance Implications
System Design Example
Backends for Frontends (BFF) Pattern
Introduction to BFF
The Problem: Traditional Backend Models
The Architecture of the BFF Pattern
BFF Pattern: An Example
Performance Implications
System Design Examples
Service Discovery Pattern
What is Service Discovery Pattern?
The Problem: Service Coordination in Distributed Systems
Service Discovery Pattern: A Solution
The Architecture of the Service Discovery Pattern
The Inner Workings of the Service Discovery Pattern
Service Discovery Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Security Considerations
Circuit Breaker Pattern
Introduction
The Problem: The Struggles of Distributed Systems and Service Failures
The Circuit Breaker Pattern: An Effective Shield Against Cascading Failures
Circuit Breaker Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Summary
Bulkhead Pattern
Introduction
The Problem: Failure Propagation in Distributed Systems
The Bulkhead Pattern: A Solution
The Architecture
The Inner Workings
Bulkhead Pattern: A Example
Performance Implications and Special Considerations
System Design Examples
Conclusion
Retry Pattern
Introduction
The Retry Pattern: A Solution to Unreliable External Resources
The Architecture of the Retry Pattern
Retry Pattern: An Example
Performance Implications
Use Cases and System Design Examples
Conclusion
Sidecar Pattern
Introduction to the Sidecar Pattern
The Problem: Monolithic Application Management
A Solution to the Monolithic Mayhem
The Architecture of the Sidecar Pattern
Sidecar Pattern: Bringing Theory to Practice with an Example
Performance Implications
System Design Examples: Bringing the Sidecar Pattern to Life
Introduction to Saga Pattern
The Problem: Traditional Transaction Models
The Saga Pattern: A Solution
The Architecture of the Saga Pattern
The Inner Workings of the Saga Pattern
Saga Pattern: A Example
Performance Implications
System Design Examples
Conclusion
Introduction
The Problem: Managing Complex Interactions in Distributed Systems
Event-Driven Architecture: A Promising Solution
The Architecture of the Event-Driven Architecture Pattern
The Inner Workings of the Event-Driven Architecture Pattern
Event-Driven Architecture Pattern: An Example
Performance Implications and Special Considerations
Use Cases and System Design Examples
Conclusion
Introduction
The Problem: Traditional CRUD Operations
CQRS Pattern: A Solution
The Architecture of the CQRS Pattern
The Inner Workings of the CQRS Pattern
CQRS Pattern: An Example
Issues, Special Considerations, and Performance Implications
System Design Examples
Introduction
The Problem: Configuration Management in a Microservices Architecture
The Solution: Configuration Externalization Pattern
Unveiling the Architecture: How Does Configuration Externalization Work?
Delving into Code: An Example
Considerations and Implications
Use Cases and Real-world Examples
Conclusion
Embrace the Future of Software Architecture
Sidecar Pattern: Bringing Theory to Practice with an Example

In this section, we'll bring the Sidecar Pattern to life, showing you just how it operates in a real-world scenario.

Creating the Main Application

First things first, we need an application. Let's create a simple Java web application. This application will expose a RESTful API and will serve as our main application.

Here is a simple Java web service using Spring Boot:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } @GetMapping("/api") public String getData() { return "Hello, Sidecar!"; } }

This web service will return the string "Hello, Sidecar!" whenever we hit the "/api" endpoint.

Introducing the Sidecar

Now that we have our main application, it's time to introduce the sidecar. In our case, let's create a sidecar that will log every request to our "/api" endpoint. For simplicity, our sidecar will also be a Java application:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class LoggingSidecar { public static void main(String[] args) { SpringApplication.run(LoggingSidecar.class, args); } @GetMapping("/log") public String logRequest() { System.out.println("Request received at /api endpoint"); return "Logged!"; } }

This sidecar will print a log message every time the "/log" endpoint is hit. We'll use this endpoint to log requests to our main application's "/api" endpoint.

Making the Sidecar and Main Application Work Together

Now, let's make our main application and sidecar work together. We need to modify our main application to call the "/log" endpoint of the sidecar whenever the "/api" endpoint is hit.

We'll use RestTemplate, a Spring utility class for calling RESTful services:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @SpringBootApplication @RestController public class MainApplication { private RestTemplate restTemplate = new RestTemplate(); public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } @GetMapping("/api") public String getData() { ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8081/log", String.class); System.out.println("Response from sidecar: " + response.getBody()); return "Hello, Sidecar!"; } }

Now, whenever the "/api" endpoint of our main application is hit, it will call the "/log" endpoint of our sidecar. The sidecar will log the request, and the main application will continue to process the request.

Keep in Mind: The Docker Factor

Remember, the sidecar and the main application need to be deployed within the same container to achieve the benefits of the Sidecar Pattern. While our Java example didn't cover this aspect, in a production scenario, you'd use a container platform like Docker to deploy the main application and the sidecar in the same container.

You'd also likely use Kubernetes as an orchestration tool to manage the deployment and scaling of your application and its sidecar. Kubernetes also has built-in support for sidecar containers, making it an ideal tool for implementing the Sidecar Pattern.

The Architecture of the Sidecar Pattern
Performance Implications
Mark as Completed

On this page

Creating the Main Application

Introducing the Sidecar

Making the Sidecar and Main Application Work Together

Keep in Mind: The Docker Factor